home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 3006 / 3006.xpi / components / dhLicenseHandler.js < prev    next >
Text File  |  2010-01-15  |  9KB  |  283 lines

  1. /******************************************************************************
  2.  *            Copyright (c) 2006-2009 Michel Gutierrez. All Rights Reserved.
  3.  ******************************************************************************/
  4.  
  5. /**
  6.  * Constants.
  7.  */
  8.  
  9. const NS_LICENSE_HANDLER_CID = Components.ID("{b60070dc-d471-4007-ab63-b30626e5ab5c}");
  10. const NS_LICENSE_HANDLER_PROG_ID = "@downloadhelper.net/license-handler;1";
  11. const DHNS = "http://downloadhelper.net/1.0#";
  12.  
  13. var Util=null;
  14.  
  15. /**
  16. * Object constructor
  17. */
  18. function LicenseHandler() {
  19.     //dump("[LicenseHandler] constructor\n");
  20.     var uriLoader = Components.classes["@mozilla.org/uriloader;1"].getService(Components.interfaces.nsIURILoader);
  21.     uriLoader.registerContentListener(this);
  22. }
  23.  
  24. LicenseHandler.prototype = {
  25.         get loadCookie() { return this.mLoadCookie; },
  26.         set loadCookie(newval) { return this.mLoadCookie=newval; },
  27.         get parentContentListener() { return this.mParentContentListener; },
  28.         set parentContentListener(newval) { return this.mParentContentListener=newval; }
  29. }
  30.  
  31. LicenseHandler.prototype.canHandleContent = function( 
  32.     contentType, 
  33.     isContentPreferred, 
  34.     desiredContentType )  {
  35.  
  36.     //dump("[LicenseHandler] canHandleContent contentType: "+contentType+"\n");
  37.  
  38.     if(contentType=="application/x-downloadhelper-license") 
  39.         return true;
  40.     else
  41.         return false;
  42.     
  43. }
  44.  
  45. LicenseHandler.prototype.doContent = function( 
  46.     contentType , 
  47.     isContentPreferred , 
  48.     request , 
  49.     contentHandler ) {
  50.     
  51.     //dump("[LicenseHandler] doContent contentType: "+contentType+"\n");
  52.  
  53.     if(contentType!="application/x-downloadhelper-license")
  54.         return false;
  55.         
  56.     function StreamListener() {
  57.         this.outputStream=null;
  58.         this.debugData=false;
  59.     }
  60.  
  61.     StreamListener.prototype={
  62.         QueryInterface: function(iid) {
  63.             if (!iid.equals(Components.interfaces.nsISupports) && 
  64.                 !iid.equals(Components.interfaces.nsIStreamListener)) {
  65.                     throw Components.results.NS_ERROR_NO_INTERFACE;
  66.                 }
  67.             return this;
  68.         },
  69.         onStartRequest: function(request,context) {
  70.  
  71.             try {
  72.  
  73.             this.httpChannel=request.QueryInterface(Components.interfaces.nsIHttpChannel);
  74.             this.responseStatus=this.httpChannel.responseStatus;
  75.             this.responseStatusText=this.httpChannel.responseStatusText;
  76.             this.contentType=this.httpChannel.getResponseHeader("content-type");            
  77.             this.data="";
  78.             
  79.             //dump("[LicenseHandler/StreamListener] onStartRequest response: "+
  80.             //    this.responseStatus+" "+this.responseStatusText+"\n");
  81.  
  82.             } catch(e) {
  83.                 dump("[LicenseHandler/StreamListener] onStartRequest error: "+e+"\n");    
  84.             }
  85.  
  86.         },
  87.         onDataAvailable: function(request,context,inputStream,offset,count) {
  88.             //dump("[LicenseHandler/StreamListener] onDataAvailable\n");    
  89.  
  90.             try {
  91.             
  92.             var sstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
  93.                    .createInstance(Components.interfaces.nsIConverterInputStream);
  94.             sstream.init(inputStream, "utf-8", 256, 
  95.                 Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  96.  
  97.             var str={};
  98.             var n=sstream.readString(128,str);
  99.             while(n>0) {
  100.                 this.data+=str.value;
  101.                 //dump("[LicenseHandler/StreamListener] onDataAvailable read: "+str.value+"\n");    
  102.                 str={};
  103.                 n=sstream.readString(128,str);
  104.             }
  105.  
  106.             } catch(e) {
  107.                 dump("[LicenseHandler/StreamListener] onDataAvailable error: "+e+"\n");    
  108.             }
  109.  
  110.         },
  111.         onStopRequest: function(request,context,nsresult) {
  112.             //dump("[LicenseHandler/StreamListener] onStopRequest\n");
  113.  
  114.             try {
  115.  
  116.             if(this.responseStatus==200) {
  117.  
  118.                 //dump("[LicenseHandler/StreamListener] parsing data: "+this.data+"\n");            
  119.                     
  120.                 var parser=Components.classes["@mozilla.org/xmlextras/domparser;1"].
  121.                     createInstance(Components.interfaces.nsIDOMParser);
  122.                 var doc=parser.parseFromString(this.data,"text/xml");
  123.                 if(doc!=null) {
  124.                     var licenseKey=Util.xpGetString(doc.documentElement,"/downloadhelper-license/license-key");
  125.                     //dump("[LicenseHandler/StreamListener] license: "+licenseKey+"\n");
  126.                     if(licenseKey!=null) {
  127.                         var convertMgr=Components.classes["@downloadhelper.net/convert-manager-component"]
  128.                             .getService(Components.interfaces.dhIConvertMgr);
  129.                         convertMgr.register(licenseKey);
  130.                     } else {
  131.                         dump("[LicenseHandler/StreamListener] no license key found: "+this.data+"\n");
  132.                     }
  133.                 } else {
  134.                     dump("[LicenseHandler/StreamListener] invalid license file: "+this.data+"\n");
  135.                 }
  136.             }
  137.             
  138.             } catch(e) {
  139.                 dump("[LicenseHandler/StreamListener] onStopRequest error: "+e+"\n");
  140.             }
  141.  
  142.         }
  143.     }
  144.     
  145.     try {
  146.         contentHandler.value=new StreamListener();
  147.     } catch(e) {
  148.         dump("[LicenseHandler] openAsync error: "+e+"\n");    
  149.     }
  150.         
  151.     return false;
  152. }
  153.  
  154. LicenseHandler.prototype.isPreferred = function( 
  155.     contentType , 
  156.     desiredContentType ) {
  157.  
  158.     //dump("[LicenseHandler] isPreferred contentType: "+contentType+"\n");
  159.  
  160.     if(contentType=="application/x-downloadhelper-license") 
  161.         return true;
  162.     else
  163.         return false;
  164.  
  165. }
  166.  
  167.  
  168. LicenseHandler.prototype.onStartURIOpen = function( URI ) {
  169.  
  170.     //dump("[LicenseHandler] onStartURIOpen: "+URI.spec+"\n");
  171.  
  172.     return false;
  173. }
  174.  
  175. LicenseHandler.prototype.GetWeakReference = function( ) {
  176.  
  177.     //dump("[LicenseHandler] GetWeakReference\n");
  178.  
  179.     return this;
  180. }
  181.  
  182.  
  183. LicenseHandler.prototype.QueryInterface = function(iid) {
  184.     //dump("[LicenseHandler] QueryInterface("+iid+")\n");
  185.     if(
  186.         iid.equals(Components.interfaces.nsISupports) ||
  187.         iid.equals(Components.interfaces.nsIURIContentListener) ||
  188.         iid.equals(Components.interfaces.nsISupportsWeakReference)
  189.     ) {
  190.         return this;
  191.         }
  192.     throw Components.results.NS_ERROR_NO_INTERFACE;
  193. }
  194.  
  195. var vLicenseHandlerModule = {
  196.     firstTime: true,
  197.     
  198.     /*
  199.      * RegisterSelf is called at registration time (component installation
  200.      * or the only-until-release startup autoregistration) and is responsible
  201.      * for notifying the component manager of all components implemented in
  202.      * this module.  The fileSpec, location and type parameters are mostly
  203.      * opaque, and should be passed on to the registerComponent call
  204.      * unmolested.
  205.      */
  206.     registerSelf: function (compMgr, fileSpec, location, type) {
  207.  
  208.         if (this.firstTime) {
  209.             this.firstTime = false;
  210.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  211.         }
  212.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  213.         compMgr.registerFactoryLocation(NS_LICENSE_HANDLER_CID,
  214.                                         "LicenseHandler",
  215.                                         NS_LICENSE_HANDLER_PROG_ID, 
  216.                                         fileSpec,
  217.                                         location,
  218.                                         type);
  219.     },
  220.  
  221.     unregisterSelf: function(compMgr, fileSpec, location) {
  222.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  223.         compMgr.unregisterFactoryLocation(NS_DH_LICENSE_HANDLER_CID, fileSpec);
  224.     },
  225.  
  226.     /*
  227.      * The GetClassObject method is responsible for producing Factory and
  228.      * SingletonFactory objects (the latter are specialized for services).
  229.      */
  230.     getClassObject: function (compMgr, cid, iid) {
  231.         if (!cid.equals(NS_LICENSE_HANDLER_CID)) {
  232.             throw Components.results.NS_ERROR_NO_INTERFACE;
  233.         }
  234.  
  235.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  236.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  237.         }
  238.  
  239.         return this.vLicenseHandlerFactory;
  240.     },
  241.  
  242.     /* factory object */
  243.     vLicenseHandlerFactory: {
  244.         /*
  245.          * Construct an instance of the interface specified by iid, possibly
  246.          * aggregating it with the provided outer.  (If you don't know what
  247.          * aggregation is all about, you don't need to.  It reduces even the
  248.          * mightiest of XPCOM warriors to snivelling cowards.)
  249.          */
  250.         createInstance: function (outer, iid) {
  251.             if (outer != null) {
  252.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  253.             }
  254.     
  255.             if(Util==null) 
  256.                 Util=Components.classes["@downloadhelper.net/util-service;1"]
  257.                     .getService(Components.interfaces.dhIUtilService);
  258.  
  259.             return new LicenseHandler().QueryInterface(iid);
  260.         }
  261.     },
  262.  
  263.     /*
  264.      * The canUnload method signals that the component is about to be unloaded.
  265.      * C++ components can return false to indicate that they don't wish to be
  266.      * unloaded, but the return value from JS components' canUnload is ignored:
  267.      * mark-and-sweep will keep everything around until it's no longer in use,
  268.      * making unconditional ``unload'' safe.
  269.      *
  270.      * You still need to provide a (likely useless) canUnload method, though:
  271.      * it's part of the nsIModule interface contract, and the JS loader _will_
  272.      * call it.
  273.      */
  274.     canUnload: function(compMgr) {
  275.         return true;
  276.     }
  277. };
  278.  
  279. function NSGetModule(compMgr, fileSpec) {
  280.     return vLicenseHandlerModule;
  281. }
  282.  
  283.